<template>
	<view class="container">
		<!-- 顶部背景图 -->
		<u-navbar
			:title="userInfo.isPartner==1 ? '申请成为合伙人' : '合伙人资料展示'" 
			:background="{backgroundColor: '#fff'}"
		></u-navbar>
		<view class="bgImg" style="position: relative;">
			<image
				class="bgImg"
				:src="userInfo.isPartner==1 ? 'http://shejihm.oss-cn-beijing.aliyuncs.com/202404081738041712569084141560281f8.png' : 'http://shejihm.oss-cn-beijing.aliyuncs.com/202404081737481712569068909ff3fe80b.png'" 
				mode=""
			></image>
		</view>
		<view class="content">
			<!-- 菜单区 -->
			<view class="content_3 flex flex_clm justify-start">
				<view class="flex_clm justify-start align-center" v-if="auditStatus==3" style="margin-top: 50rpx;">
					<image style="width: 136rpx;height: 136rpx;" src="http://shejihm.oss-cn-beijing.aliyuncs.com/2024041010401217127168123962e823624.png" mode=""></image>
					<text>您的申请已被拒绝</text>
					<view class="flex justify-start align-start" style="line-height: 40rpx !important;margin-bottom: 52rpx;">
						<text style="font-size: 28rpx;font-weight: 500;color: #999;flex-shrink: 0;">拒绝原因: </text>
						<text style="font-size: 28rpx;font-weight: 500;color: #999;">{{ failReason }}</text>
					</view>
				</view>
				<view v-else class="menu_title">{{userInfo.isPartner==1 ? auditStatus==1 ? '正在审核中':'填写申请资料' : '资料展示'}}</view>
				
				<view :style="auditStatus==3||auditStatus==1 ? 'background:#f7f7f7' : 'background:#fff'" style="border-radius: 16rpx;padding: 0 30rpx;">
					<!-- 菜单项 -->
					<u-form :model="queryParams" ref="form1" :label-style="labelStyle" label-width="200rpx">
						<u-form-item label="姓名:" prop="partnerName">
							<u-input 
								v-model="queryParams.partnerName" 
								input-align="right" 
								:maxlength="10"
								placeholder="请输入"
								:disabled="userInfo.isPartner==2 || auditStatus==3 || auditStatus==1"
								:placeholder-style="placeholderStyle"
							/>
						</u-form-item>
						<u-form-item label="手机号:" prop="partnerPhone">
							<u-input 
								v-model="queryParams.partnerPhone" 
								input-align="right" 
								placeholder="请输入" 
								:disabled="userInfo.isPartner==2 || auditStatus==3 || auditStatus==1"
								:placeholder-style="placeholderStyle"
							/>
						</u-form-item>
						<u-form-item
							v-if="userInfo.isPartner==1 && auditStatus=='0'"
							:label="item.dictLabel + ':'" 
							:prop="item.dictValue" 
							v-for="(item, index) in dicDataList" 
							:key="index"
						>
							<u-input 
								v-model="stringParams[item.dictValue]" 
								input-align="right" 
								:maxlength="10"
								placeholder="请输入" 
								:placeholder-style="placeholderStyle"
							/>
						</u-form-item>
						<u-form-item 
							:label="item.label + ':'" 
							v-for="(item, index) in alreadyList" 
							:key="index"
							v-else
						>
							<u-input 
								v-model="item.value" 
								input-align="right" 
								disabled
								placeholder="请输入" 
								:placeholder-style="placeholderStyle"
							/>
						</u-form-item>
					</u-form>
				</view>
				<view
					style="margin-top: 88rpx;width: 100%;"
					v-if="userInfo.isPartner==1&&auditStatus!=1"
				>
					<u-button v-if="auditStatus==3" class="logoutBtn" @click.stop="auditStatus='0'">重新编辑</u-button>
					<u-button v-else class="logoutBtn" @click.stop="submitApply">提交申请</u-button>
				</view>
			</view>
			<!-- <view 
				class="argeement flex justify-center align-center" 
				@tap="changeSelect"
				v-if="userInfo.isPartner==1"
			>
				<image :src="selectBtn ? '/static/images/icon_add_select.png' : '/static/images/icon_add_default.png'" mode=""></image>
				<text>我已认真阅读并同意</text>
				<text @tap.stop="toHelpArgeement">《申请合伙人协议》</text>
			</view> -->
			
		</view>
	</view>
</template>

<script setup>
import {getCurrentInstance, ref } from 'vue'
import { onLoad } from "@dcloudio/uni-app"
const {proxy} = getCurrentInstance()
	const showLogin = ref(false)
	const selectBtn = ref(false)
	const dicDataList = ref([])
	const queryParams = ref({})
	const userInfo = ref([])
	const auditStatus = ref('0') //0:未申请过
	const failReason = ref('')  //拒绝原因
	const stringParams = ref({}) //字典动态配置的请求参数
	const alreadyList = ref([])  //已申请的数据
	// isPartner 是否合伙人 1否 2是
	onLoad(()=>{
		auditStatus.value = '0'
		userInfo.value = uni.getStorageSync('USER_INFO')
		// if(userInfo.value.isPartner == 2){   //不管状态, 都直接查
			proxy.api.getPartnerRecord().then(res=>{
				if(res&&res.data&&res.data.data){
					queryParams.value = {
						partnerName: res.data.data.partnerName,
						partnerPhone: res.data.data.partnerPhone,
					}
					failReason.value = res.data.data.failReason //拒绝原因
					auditStatus.value = res.data.data.auditStatus
					alreadyList.value = JSON.parse(res.data.data.applyInformation)
				}
			})
		// }
		proxy.api.system({code: 'apply'}).then(res=>{
			dicDataList.value = res.data.data
		})
	})
	const placeholderStyle = ref({
		fontFamily: 'PingFang SC',
		fontWeight: '400',
		fontSize: '28rpx',
		color: '#999999',
		lineHeight: '36rpx',
	})
	const labelStyle = ref({
		fontFamily: 'PingFang SC',
		fontWeight: 'bold',
		fontSize: '28rpx',
		color: '#333333',
		lineHeight: '36rpx',
	})

	// 切换是否选中协议
	function changeSelect() {
		selectBtn.value = !selectBtn.value
	}
	// 提交申请
	function submitApply() {
		let applyInformationArr = ref([])
		for(let key in stringParams.value){
			applyInformationArr.value.push({
				label: key,
				value: stringParams.value[key]
			})
		}
		if(!proxy.utils.isMobile(queryParams.value.partnerPhone)){
			proxy.tui.toast('手机号格式不正确')
			return
		} else if(proxy.utils.isNullOrEmpty(queryParams.value.partnerName)){
			proxy.tui.toast('请输入姓名')
			return
		} else {
			console.log('i', applyInformationArr.value)
			let i = dicDataList.value.every(item=>{
				console.log('i', item)
				let have = applyInformationArr.value.find(innerItem=>innerItem.label==item.dictValue)
				return have && proxy.utils.isNullOrEmpty(have.val)
			})
			if(!i){
				proxy.tui.toast('请将选项填写完整')
				return
			}
		}
		let query = {
			...queryParams.value,
			applyInformation: JSON.stringify(applyInformationArr.value)
		}
		console.log(query)
		proxy.api.applyPartner(query).then(res=>{
			uni.$emit('handle_apply')
			uni.navigateBack({delta: 1})
		})
	}
</script>

<style lang="scss" scoped>
	.logoutBtn ::v-deep .u-btn--default, .u-btn--default-hover{
		background-color: $uni-color-primary !important;
		color: #fff !important;
		line-height: 80rpx !important;
		border-radius: 40rpx;
	}
	.bgImg {
		width: 100vw;
		height: 422rpx;
		position: relative;
	}

	.avatar-wrapper {
		padding: 0;
		margin: 0;
		width: 60rpx;
		height: 60rpx;
		background-color: raga(0, 0, 0, 0);
	}

	.container {
		width: 100vw;
		height: 100vh;
		overflow-y: scroll;
		background-color: $uni-bg-color-grey;

		.content {
			width: calc(100vw - 40rpx);
			margin-left: 20rpx;
			position: relative;
			margin-top: -180rpx;
			.content_3{
				padding: 20rpx 22rpx;
				padding-bottom: 50rpx;
				box-sizing: border-box;
				width: 100%;
				border-radius: 20rpx;
				margin: 20rpx 0;
				background-color: #fff;
				display: flex;
				text{
					@include str(32rpx, bold, #333);
				}
				&>view{
					line-height: 100rpx;
				}
				.menu_title{
					@include str(36rpx, 800, #333);
					line-height: 150rpx;
					width: 100%;
					text-align: center;
				}
			}
			.argeement {
				margin-top: 58rpx;
				image {
					width: 32rpx;
					height: 32rpx;
					margin-right: 10rpx;
				}
				text:nth-child(2) {
					@include str(28rpx, 500, #333);
				}
				text:nth-child(3) {
					@include str(28rpx, 500, $uni-color-primary);
				}
			}
		}
	}
</style>